package YUtils;

public abstract class ColorUtils {
    public static int fromARGB(int alpha,int red,int green,int blue){
        return (alpha << 24) | (red << 16) | (green << 8) | blue;
    }
    public static int fromARGB(float alpha, float red, float green, float blue) {
        return ((int) (alpha * 255.0f + 0.5f) << 24) |
                ((int) (red   * 255.0f + 0.5f) << 16) |
                ((int) (green * 255.0f + 0.5f) <<  8) |
                (int) (blue  * 255.0f + 0.5f);
    }
    public static int fromRGB(int red,int green,int blue){
        return 0xff000000 | (red << 16) | (green << 8) | blue;
    }
    public static int fromRGB(float red, float green, float blue) {
        return  0xff000000 |
                ((int) (red   * 255.0f + 0.5f) << 16) |
                ((int) (green * 255.0f + 0.5f) <<  8) |
                (int) (blue  * 255.0f + 0.5f);
    }
    public static int r(int color) {
        return (color >> 16) & 0xFF;
    }
    public static int g(int color) {
        return (color >> 8) & 0xFF;
    }
    public static int b(int color) {
        return color & 0xFF;
    }
    public static int parseColor(String colorString) {
        long color;
        if (colorString.charAt(0) == '#') {
            color = Long.parseLong(colorString.substring(1), 16);
            if (colorString.length() == 7) {
                // Set the alpha value
                color |= 0x00000000ff000000;
            }else if(colorString.length()!=9){
                throw new IllegalArgumentException("Unknown color");
            }
        }else if(colorString.startsWith("0x")){
            color = Long.parseLong(colorString.substring(2), 16);
            if (colorString.length() == 8) {
                // Set the alpha value
                color |= 0x00000000ff000000;
            }else if(colorString.length()!=10){
                throw new IllegalArgumentException("Unknown color");
            }
        }else{
            throw new IllegalArgumentException("Unknown color");
        }
        return (int)color;
    }
}
